import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _map from "lodash/map";
import _forEach from "lodash/forEach";
import _cloneDeep from "lodash/cloneDeep";
import { commonParseForField } from "./common";
/**
 * 将 存储的 dynamicToolbar 转换成 设计器 可以识别的
 * @param {object} template
 * @param {object} options
 * @returns {object}
 */

function dynamicToolbarDirtyExtra(template, options) {
  var noCloneDeep = options && options.noCloneDeep;
  var newTemplate = noCloneDeep ? template : _cloneDeep(template);
  newTemplate.fields = _map(newTemplate.fields, function (field) {
    var props = {};

    _forEach(field.config, function (prop) {
      props[prop.attributeName] = prop.value;
    });

    return _objectSpread(_objectSpread({}, field), {}, {
      type: props.type,
      style: {
        marginRight: props['style.marginRight']
      }
    });
  });
  return newTemplate;
}

function dynamicToolbarDirtyParse(template, options) {
  var noCloneDeep = options && options.noCloneDeep;
  var newTemplate = noCloneDeep ? template : _cloneDeep(template);
  var orderSeq = 0;
  var orderSeqStep = 10;
  var newFields = [];

  _forEach(newTemplate.fields, function (field) {
    commonParseForField(newTemplate, field);
    delete field.style; // warn no style on field

    delete field.type; // warn no type on field

    newFields.push(_objectSpread(_objectSpread({}, field), {}, {
      orderSeq: orderSeq += orderSeqStep,
      // todo toolbar是否没有这两个字段
      enabledFlag: 1,
      requiredFlag: 0
    }));
  });

  return _objectSpread(_objectSpread({}, newTemplate), {}, {
    fields: newFields
  });
}

function dynamicToolbarInitExtra(template, options) {
  var noCloneDeep = options && options.noCloneDeep;
  var newTemplate = noCloneDeep ? template : _cloneDeep(template);
  return newTemplate;
}
/**
 * 新加进来的 字段 宽度统一为 120
 * @param {object} template
 * @param {object} field
 * @param {object} options
 * @returns
 */
// eslint-disable-next-line no-unused-vars


function dynamicToolbarInitExtraField(template, field, options) {}

var dynamicToolbar = {
  initExtra: dynamicToolbarInitExtra,
  initExtraField: dynamicToolbarInitExtraField,
  dirtyExtra: dynamicToolbarDirtyExtra,
  dirtyParse: dynamicToolbarDirtyParse
};
export default dynamicToolbar;